﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LongestFlags.Question;

namespace LongestFlags.Answer
{
    class Albin : ILongestFlag
    {
        public struct Shu
        {
            public int postion;
            public int num;
        }
        public int GetLongestFlags(int[] iArray, out int startIndex, out int endIndex)
        {
            Stack<Shu> st = new Stack<Shu>();
            Shu sk = new Shu();
            Shu su = new Shu();
            int[] temp = new int[iArray.Length];
            int count = 0;
            startIndex = 0;
            endIndex = 0;
            int get = 0;
            if (iArray.Length == 1)
            {
                if (iArray[0] != 0)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
            for (int i = 0; i < iArray.Length; i++)
            {
                if (st.Count > 0)
                {
                    if (iArray[i] > 0)
                    {
                        sk = st.Peek();
                        if (sk.num == -1)
                        {
                            temp[sk.postion] = 1;
                            temp[i] = 1;
                            st.Pop();
                        }
                        else
                        {
                            su.num = 1;
                            su.postion = i;
                            st.Push(su);
                        }

                    }
                    if (iArray[i] < 0)
                    {
                        sk = st.Peek();
                        if (sk.num == 1)
                        {
                            temp[sk.postion] = 1;
                            temp[i] = 1;
                            st.Pop();
                        }
                        else
                        {
                            su.num = -1;
                            su.postion = i;
                            st.Push(su);
                        }
                    }
                    if (iArray[i] == 0)
                    {
                       temp[i] = 1;
                    }
                }
                else
                {
                    if (iArray[i] > 0)
                    {
                        su.num = 1;
                        su.postion = i;

                        st.Push(su);
                    }
                    if (iArray[i] < 0)
                    {
                        su.num = -1;
                        su.postion = i;
                        st.Push(su);
                    }
                    if (iArray[i] == 0)
                    {
                       temp[i] = 1;
                    }

                }
            }
            for (int i = 0; i < iArray.Length; i++)
            {
                if (get + temp[i] > get)
                {
                    get++;
                    if (get > count)
                    {
                        count = get;
                        endIndex = i;
                    }
                }
                else
                {
                    get = 0;
                }
            }
            if (count > 0)
            {
                startIndex = endIndex - count + 1;
            }
            return count;
        }
       
    }
}
